package com.example.Administrator.shoujiweishi.activities;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import com.example.Administrator.shoujiweishi.R;
import com.example.Administrator.shoujiweishi.activities.adapter.MyRecalAdapter;
import com.socks.library.KLog;
import com.example.Administrator.shoujiweishi.MyApplication;
import com.example.Administrator.shoujiweishi.activities.base.BaseActivity;
import com.example.Administrator.shoujiweishi.activities.helper.OnStartDragListener;
import com.example.Administrator.shoujiweishi.activities.helper.SimpleItemTouchHelperCallback;
import com.example.Administrator.shoujiweishi.db.BBXDao;
import com.example.Administrator.shoujiweishi.vo.BBX;

import java.util.Collections;
import java.util.List;

public class BBXaa extends BaseActivity implements OnStartDragListener{

    RecyclerView recyclerView;
    MyRecalAdapter myAdapter;
    private ItemTouchHelper mItemTouchHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        recyclerView= (RecyclerView) findViewById(R.id.recal);
        //设定布局管理器
        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,4);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        recyclerView.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL));



        BBXDao bbxDao=MyApplication.getDaoSession().getBBXDao();
        final List<BBX> list=bbxDao.loadAll();
//        Log.d("saddsa",list.get(2).getUsername()+"");
        KLog.e(list.size());
        myAdapter = new MyRecalAdapter(list,this);
        recyclerView.setAdapter(myAdapter);

        ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(myAdapter);
        mItemTouchHelper = new ItemTouchHelper(callback);
        mItemTouchHelper.attachToRecyclerView(recyclerView);
        ItemTouchHelper helper =new ItemTouchHelper(new ItemTouchHelper.Callback() {
            @Override
            public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
                int dragFlags = ItemTouchHelper.UP|ItemTouchHelper.DOWN;//拖拽
                int swipeFlags = ItemTouchHelper.LEFT|ItemTouchHelper.RIGHT;//侧滑删除
                return makeMovementFlags(dragFlags,swipeFlags);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                Collections.swap(list,viewHolder.getAdapterPosition(),target.getAdapterPosition());
                myAdapter.notifyItemMoved(viewHolder.getAdapterPosition(),target.getAdapterPosition());
                return false;
            }

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
                list.remove(viewHolder.getAdapterPosition());
                myAdapter.notifyItemRemoved(viewHolder.getAdapterPosition());
            }

            @Override
            public boolean isLongPressDragEnabled() {
                return true;
            }
        });
        helper.attachToRecyclerView(recyclerView);
    }

    @Override
    public int getContentXML() {
        return R.layout.activity_bbxaa;
    }

    @Override
    public void initData() {

    }

    @Override
    public void initView() {

    }

    @Override
    public void onStartDrag(RecyclerView.ViewHolder viewHolder) {
        mItemTouchHelper.startDrag(viewHolder);
    }
}
